package com.wavehi.dao.impl;

import com.wavehi.dao.IUserDAO;
import com.wavehi.pojo.User;
import com.wavehi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

/**
 * @Author WaveHi
 * @Date 3/9/2021 下午 5:17
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class UserDAOImpl {
    public void insert(User user) {
//        //获取连接
//        SqlSession connection = MyBatisUtils.getConnection();
//        //执行
//        connection.insert("com.wavehi.mapper.UserMapper.insert",user);
//        //提交
//        connection.commit();
//        //资源的关闭
//        connection.close();
        //映射文件配置接口后
        SqlSession conn = MyBatisUtils.getConnection();
        //获取接口代理对象
        IUserDAO mapper = conn.getMapper(IUserDAO.class);
        //执行
        mapper.insert(user);
        //资源的关闭
        conn.close();

    }

    public User select(String username) {
        SqlSession connection = MyBatisUtils.getConnection();
        User user = connection.selectOne("com.wavehi.mapper.UserMapper.selectUser", username);
        connection.close();
        return user;
    }

    public User selectById(Integer id) {
        //获取数据库连接
        SqlSession connection = MyBatisUtils.getConnection();
        //获取接口代理对象
        IUserDAO mapper = connection.getMapper(IUserDAO.class);
        //执行
        User user = mapper.selectById(1);
        //资源的关闭
        connection.close();
        //返回结果
        return user;
    }
}
